<extend name="Common@Layout:layui-admin" />
<block name="title">
    <title>备份数据库-Administator管理后台</title>
</block>
<block name="style">
    <style>
        body {
            padding: 10px;
        }
    </style>
</block>

<block name="main">

    <div class="my-btn-box">
        <span class="fl">
            <a id="export" class="layui-btn layui-btn-danger radius btn-delect" autocomplete="off" href="javascript:;">立即备份</a>
            <a id="optimize" class="layui-btn btn-add btn-default" href="{:U('optimize')}">备份数据库</a>
            <a id="repair"  class="layui-btn layui-btn-normal" class="btn" href="{:U('repair')}">修复表</a>
        </span>
    </div>
    <div class="dataTables_wrapper no-footer">
        <form id="export-form" method="post" action="{:U('export')}">
            <table class="layui-table layui-form dataTable no-footer" role="grid">
                <colgroup>
                    <col width="50">
                    <col width="100">
                    <col width="170">
                    <col width="90">
                    <col>
                    <col>
                    <col>
                </colgroup>
                <thead>
                    <tr>
                        <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                        <th>表名</th>
                        <th>数据量</th>
                        <th>数据大小</th>
                        <th>创建时间</th>
                        <th>备份状态</th>
                        <th>操作</th>
                    </tr>

                </thead>
                <tbody class="data">
                    <volist name="list" id="table">
                        <tr>
                            <td>
                                <input type="checkbox" name="tables[]" value="{$table.name}" lay-skin="primary">
                            </td>
                            <td>{$table.name}</td>
                            <td>{$table.rows}</td>
                            <td>{$table.data_length|format_bytes}</td>
                            <td>{$table.create_time}</td>
                            <td class="info">未备份</td>
                            <td class="action">
                                <a class="layui-btn layui-btn-small ajax-get no-refresh" href="javascript:;" data-url="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                                <a class="layui-btn layui-btn-small ajax-get no-refresh" href="javascript:;" data-url="{:U('repair?tables='.$table['name'])}">修复表</a>
                            </td>
                        </tr>
                    </volist>
                </tbody>
            </table>
        </form>
    </div>

</block>

<block name="script">
    <script type="text/javascript">
        layui.use(['jquery'], function() {
            var $ = layui.jquery,
                jquery = layui.jquery;

            var $form = $("#export-form"),
                $export = $("#export"),
                tables
            $optimize = $("#optimize"), $repair = $("#repair");

            $optimize.add($repair).click(function() {
                $.post(this.href, $form.serialize(), function(data) {
                    if (data.status) {
                        updateAlert(data.info, 'alert-success');
                    } else {
                        updateAlert(data.info, 'alert-error');
                    }
                    setTimeout(function() {
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled', false);
                    }, 1500);
                }, "json");
                return false;
            });

            $export.click(function() {
                $export.parent().children().addClass("disabled");
                $export.html("正在发送备份请求...");
                $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function(data) {
                        if (data.status) {
                            tables = data.tables;
                            $export.html(data.info + "开始备份，请不要关闭本页面！");
                            backup(data.tab);
                            window.onbeforeunload = function() {
                                return "正在备份数据库，请不要关闭！"
                            }
                        } else {
                            updateAlert(data.info, 'alert-error');
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");
                            setTimeout(function() {
                                $('#top-alert').find('button').click();
                                $(that).removeClass('disabled').prop('disabled', false);
                            }, 1500);
                        }
                    },
                    "json"
                );
                return false;
            });

            function backup(tab, status) {
                status && showmsg(tab.id, "开始备份...(0%)");
                $.get($form.attr("action"), tab, function(data) {
                    if (data.status) {
                        showmsg(tab.id, data.info);

                        if (!$.isPlainObject(data.tab)) {
                            $export.parent().children().removeClass("disabled");
                            $export.html("备份完成，点击重新备份");
                            window.onbeforeunload = function() {
                                return null
                            }
                            return;
                        }
                        backup(data.tab, tab.id != data.tab.id);
                    } else {
                        updateAlert(data.info, 'alert-error');
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function() {
                            $('#top-alert').find('button').click();
                            $(that).removeClass('disabled').prop('disabled', false);
                        }, 1500);
                    }
                }, "json");

            }

            function showmsg(id, msg) {
                $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
            }

            function updateAlert(info, type) {
                if (type == 'alert-success') {
                    layer.msg(info, {
                        icon: 1,
                        time: 1000
                    });
                } else {
                    layer.msg(info, {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    </script>
</block>